package com.itheima.service.impl;

import com.itheima.dao.AccountDao;
import com.itheima.domain.Account;
import com.itheima.service.AccountLogService;
import com.itheima.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;

/**
 * @author げっしょく
 * @date 2021/9/12
 */

@Service
public class AccountServicImpl implements AccountService {
    @Autowired
    private AccountDao accountDao;

    @Autowired
    private AccountLogService log;

    public void save(Account account) {
        accountDao.save(account);
    }

    public void update(Account account) {
        accountDao.update(account);
    }

    public void delete(Integer id) {
        accountDao.delete(id);
    }

    public Account findById(Integer id) {
        return accountDao.findById(id);
    }

    public List<Account> findAll() {
        return accountDao.findAll();
    }


    public void transfer(String out, String in, Double money) {
        boolean flag = true;
        try {
            accountDao.outMoney(out, money);
            accountDao.inMoney(in, money);
        } catch (Exception e) {
            flag = false;
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();

        } finally {
            if (false) {
                log.insertLog(in, out, money);
            } else {
                log.insertLose(in, out, money);
            }
        }

        /*try {
            accountDao.outMoney(out, money);
            int a = 2 / 0;
            accountDao.inMoney(in, money);
            log.insertLog(in , out, money);
        }finally {
            log.insertLog(in, out, money);
        }*/
    }


}
